package com.qianbao.qianbaobusiness.ui.support.wechatprogram;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.androidbase.utils.ImageUtils;
import com.baseandroid.config.Global;
import com.baseandroid.mvp.BaseMvpActivity;
import com.bumptech.glide.Glide;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.MerchantInfo;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

public class WechatProgramActivty extends BaseMvpActivity<WechatProgramView, WechatProgramPresenter> implements WechatProgramView {

    @BindView(R.id.tv_title)
    TextView       mTvTitle;
    @BindView(R.id.iv_image_id)
    ImageView      mIvImageId;
    @BindView(R.id.btn_open_wxapp_id)
    Button         mBtnOpenWxappId;
    @BindView(R.id.rl_wxapp_not_open_id)
    RelativeLayout mRlWxappNotOpenId;
    @BindView(R.id.tv_shop_name_id)
    TextView       mTvShopNameId;
    @BindView(R.id.ll_shop_selector_id)
    LinearLayout   mLlShopSelectorId;
    @BindView(R.id.tv_openwx_name_id)
    TextView       mTvOpenwxNameId;
    @BindView(R.id.iv_openwx_image_id)
    ImageView      mIvOpenwxImageId;
    @BindView(R.id.ll_openwx_sava_id)
    LinearLayout   mLlOpenwxSavaId;
    @BindView(R.id.btn_save_image_id)
    Button         mBtnSaveImageId;
    @BindView(R.id.rl_wxapp_open_id)
    RelativeLayout mRlWxappOpenId;
    private String mMerchantNo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wechat_program_activty;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        mTvTitle.setText("微信小程序");
        Glide.with(this)
                .asBitmap()
                .load(R.drawable.img_wechat_qr)
                .into(mIvOpenwxImageId);
        mMerchantNo = Global.getWechatAppSelectedMerchant();
        if (TextUtils.isEmpty(mMerchantNo)) {
            mMerchantNo = Global.getWechatAppInfos().get(0).merchantNo;
        }
        if (Global.getWechatAppInfos().size() <= 1) {
            mLlShopSelectorId.setVisibility(View.GONE);
        }
        updateMerchantInfo();
        getPresenter().checkWechatQrcode();
    }

    private void updateMerchantInfo() {
        for (MerchantInfo merchantInfo : Global.getShopListInfo().merchantList) {
            if (merchantInfo.merchantNo.equals(mMerchantNo)) {
                mTvShopNameId.setText(merchantInfo.merchantName);
                mTvOpenwxNameId.setText(merchantInfo.merchantName);
                return;
            }
        }
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {

    }

    @OnClick({R.id.iv_back_toolbar, R.id.btn_open_wxapp_id, R.id.btn_save_image_id, R.id.ll_shop_selector_id})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back_toolbar:
                finish();
                break;
            case R.id.btn_open_wxapp_id:
                getPresenter().creatQrcode(mMerchantNo);
                break;
            case R.id.btn_save_image_id:
                saveBitmapToPhotoDir();
                break;
            case R.id.ll_shop_selector_id:
                Intent intent = new Intent();
                intent.putExtra("MerchantNo", mMerchantNo);
                intent.setClass(this, WechatSwitchShopActivity.class);
                startActivityForResult(intent, 0x101);
                break;
            default:
                break;
        }
    }

    private void saveBitmapToPhotoDir() {
        String dirstrString = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                                      .getAbsolutePath() + File.separator + "WechatQrCode" + mMerchantNo;
        String filePath = ImageUtils.saveBitmap2File(dirstrString, ImageUtils.view2Bitmap(mLlOpenwxSavaId));
        Uri uri = Uri.fromFile(new File(filePath));
        sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", uri));
        Toast.makeText(this, "保存至相册成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void checkQrcodeUI(boolean issigned) {
        if (issigned) {
            mRlWxappNotOpenId.setVisibility(View.GONE);
            mRlWxappOpenId.setVisibility(View.VISIBLE);
            getPresenter().getWechatQrcode(mMerchantNo);
        } else {
            mRlWxappNotOpenId.setVisibility(View.VISIBLE);
            mRlWxappOpenId.setVisibility(View.GONE);
        }
    }

    @Override
    public void updateQrcodeUi(String qrcode) {
        mRlWxappNotOpenId.setVisibility(View.GONE);
        mRlWxappOpenId.setVisibility(View.VISIBLE);
        if (this != null) {
            try {
                Glide.with(this)
                        .asBitmap()
                        .load(qrcode)
                        .into(mIvOpenwxImageId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 0x101) {
                mMerchantNo = data.getStringExtra("MerchantNo");
                Glide.with(this)
                        .asBitmap()
                        .load(R.drawable.img_wechat_qr)
                        .into(mIvOpenwxImageId);
                updateMerchantInfo();
                getPresenter().getWechatQrcode(mMerchantNo);
            }
        }
    }
}
